﻿using Microsoft.Win32;

namespace SfdClient
{
    public class RegistryUtility
    {
        public const string Regkeyname = "SfdClient";

        /// <summary>
        /// Enregistre dans la base de registre que l'application doit démarrer au lancement de Windows ou non
        /// </summary>
        /// <param name="show"></param>
        public static void SetWindowsStartValue(bool show)
        {
            System.Reflection.Assembly ass = System.Reflection.Assembly.GetExecutingAssembly();
            var regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (regKey == null)
                return;

            if (!show)
                regKey.DeleteValue(Regkeyname);
            else
                regKey.SetValue(Regkeyname, ass.Location);
        }

        /// <summary>
        /// Indique si l'application doit se lancer au démarrage de Windows ou non
        /// </summary>
        /// <returns></returns>
        public static bool GetWindowsStartValue()
        {
            var regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                                                                 true);
            if (regKey == null)
                return false;
            var regKeyValue = regKey.GetValue(Regkeyname);

            return regKeyValue != null && regKeyValue.ToString() == System.Reflection.Assembly.GetExecutingAssembly().Location;
        }

    }
}
